package cn.edu.seu.cose.qol.model.dao.config;

import java.io.IOException;

import cn.edu.seu.cose.qol.util.EnvironmentVariable;


public class DbConnCfgAccessImpl implements DbConnCfgAccess {
	
	private static final String DATABASE_CONN_CONFIG_FILE_PATH =
			"config\\database_conn.properties";
	
	private static final String ID_DRIVER= "database.driver";
	
	private static final String ID_URL= "database.url";
	
	private static final String ID_USERNAME = "database.username";
	
	private static final String ID_PASSWORD = "database.password";
	
	private static DbConnCfgAccessImpl _singleton;
	
	private PropertiesReader _reader;
	
	private PropertiesWriter _writer;
	
	public static DbConnCfgAccessImpl getInstance() throws IOException {
		if (_singleton == null) {
			EnvironmentVariable eV = EnvironmentVariable.getInstance();
			String webContentPath = eV.get(EnvironmentVariable.REAL_PATH);
			_singleton = new DbConnCfgAccessImpl(webContentPath);
		}
		
		return _singleton;
	}
	
	protected DbConnCfgAccessImpl(String webContentPath)
			throws IOException {
		String realPath = webContentPath + DATABASE_CONN_CONFIG_FILE_PATH;
		_reader = PropertiesReader.getInstance(realPath);
		_writer = PropertiesWriter.getInstance(realPath);
	}
	
	@Override
	public void setDriver(String driverName) throws IOException {
		_writer.setProperty(ID_DRIVER, driverName);
	}

	@Override
	public String getDriver() {
		return _reader.getProperty(ID_DRIVER);
	}

	@Override
	public void setUrl(String url) throws IOException {
		_writer.setProperty(ID_URL, url);
	}

	@Override
	public String getUrl() {
		return _reader.getProperty(ID_URL);
	}

	@Override
	public void setUsername(String username) throws IOException {
		_writer.setProperty(ID_USERNAME, username);
	}

	@Override
	public String getUsername() {
		return _reader.getProperty(ID_USERNAME);
	}

	@Override
	public void setPassword(String password) throws IOException {
		_writer.setProperty(ID_PASSWORD, password);
	}

	@Override
	public String getPassword() {
		return _reader.getProperty(ID_PASSWORD);
	}

}
